package com.fingard.dsp.bank.directbank.lcbip01;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/10/9.
 */
public class LCBIP01Deposit extends LCBIP01Base implements IDeposit {

    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception{
        RetAts7507DepQry retAtsTrans = new RetAts7507DepQry(p_req7507.reqHeader);//返回报文
        retAtsTrans.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo ="";

        try{
            //非公有参数
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            //拼接请求报文
            LCBIP01Header header = new LCBIP01Header();
            header.TuxName="QueryDepositAgreement";
            header.ReqSeqNo = getQryTransID();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeEndElement();
            XElement body =new XElement("body","");
            body.addChild(new XElement("CustCode",bankFront.org));//客户编号
            body.addChild(new XElement("CustName",""));//客户名称
            body.addChild(new XElement("ProtocolNo",""));//存单号
            body.addChild(new XElement("ProtocolType",""));//协议类型
            body.addChild(new XElement("DepositProName",""));//存款产品
            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();
            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
            //测试
            //bankResult[0]="";
            //bankResult[1]="<?xml version=\"1.0\" encoding=\"gb2312\"?><lcbank>    <head>        <BankNo>50</BankNo>        <TuxName>QueryDepositAgreement</TuxName>        <ReqSeqNo>00002041</ReqSeqNo>        <RespSeqNo></RespSeqNo>        <RespDate>20181012</RespDate>        <RespTime>152946</RespTime>        <RespCode>0</RespCode>        <RespInfo>协议查询成功</RespInfo>        <RecordNum></RecordNum>        <ConvertedReqSeqNo>00002041</ConvertedReqSeqNo>    </head>    <body>        <ProtoQueryRespParam>            <Amount>100000000</Amount>            <Currency>01</Currency>            <DepositName></DepositName>            <ExpireDate>2009-12-29</ExpireDate>            <IsBespoke>0</IsBespoke>            <NotifyType></NotifyType>            <PledgeFlagName></PledgeFlagName>            <ProtocolCode>ETL_XY_1010300820320081</ProtocolCode>            <ProtocolType>01</ProtocolType>            <Rate>1.71</Rate>            <RemaidAmt>核心移植-定期协议-协议编号1010300820320081</RemaidAmt>            <StartAccualDate>2009-09-29</StartAccualDate>            <StartDate>2009-09-29</StartDate>            <Status>7</Status>            <Term>1月</Term>        </ProtoQueryRespParam>        <ProtoQueryRespParam>            <Amount>50000000</Amount>            <Currency>01</Currency>            <DepositName></DepositName>            <ExpireDate>2009-12-29</ExpireDate>            <IsBespoke>0</IsBespoke>            <NotifyType></NotifyType>            <PledgeFlagName></PledgeFlagName>            <ProtocolCode>ETL_XY_1010300820320082</ProtocolCode>            <ProtocolType>01</ProtocolType>            <Rate>1.71</Rate>            <RemaidAmt>核心移植-定期协议-协议编号1010300820320082</RemaidAmt>            <StartAccualDate>2009-09-29</StartAccualDate>            <StartDate>2009-09-29</StartDate>            <Status>7</Status>            <Term>1月</Term>        </ProtoQueryRespParam>        <ProtoQueryRespParam>            <Amount>50000000</Amount>            <Currency>01</Currency>            <DepositName></DepositName>            <ExpireDate>2009-12-29</ExpireDate>            <IsBespoke>0</IsBespoke>            <NotifyType></NotifyType>            <PledgeFlagName></PledgeFlagName>            <ProtocolCode>ETL_XY_1010300820320083</ProtocolCode>            <ProtocolType>01</ProtocolType>            <Rate>1.71</Rate>            <RemaidAmt>核心移植-定期协议-协议编号1010300820320083</RemaidAmt>            <StartAccualDate>2009-09-29</StartAccualDate>            <StartDate>2009-09-29</StartDate>            <Status>7</Status>            <Term>1月</Term>        </ProtoQueryRespParam>    </body></lcbank>\n";
            //处理响应报文
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1].trim(),tmpSbMsg);
                if (tmpBkReader != null) {
                    String RespCode = tmpBkReader.getFirstText("/lcbank/head/RespCode");
                    String RespInfo = tmpBkReader.getFirstText("/lcbank/head/RespInfo");
                    //交易成功
                    if("0".equals(RespCode)){
                        while (tmpBkReader.read("/lcbank/body/ProtoQueryRespParam")){
                            XElement readXElement = tmpBkReader.readedXElement;
                            RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                            retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                            retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                            retAts7507Detail.depositCode=readXElement.getSingleTextNotNull("ProtocolCode").trim();//定存单号
                            String protocolType=readXElement.getSingleTextNotNull("ProtocolType").trim();
                            if(!"".equals(protocolType) && "01".equals(protocolType)){//内部定期协议
                                retAts7507Detail.startDate=readXElement.getSingleTextNotNull("StartDate");//起始日期
                            }else if(!"".equals(protocolType) && "08".equals(protocolType)){//内部通知存款协议
                                retAts7507Detail.startDate=readXElement.getSingleTextNotNull("StartAccualDate");//起始日期
                            }else {
                                tmpRespCode = "-1";
                                tmpRespInfo = "内部定期协议异常";
                                break;
                            }
                            retAts7507Detail.endDate=readXElement.getSingleTextNotNull("ExpireDate").trim();//截止日期
                            retAts7507Detail.valueDate=retAts7507Detail.startDate;//起息日期
                            String terms=readXElement.getSingleTextNotNull("Term").trim();//期限
                            retAts7507Detail.terms=getBankConfig().getMapValueByKey("termMapping",terms);//存款期限 (七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                            retAts7507Detail.currencyID=convertCurToATS(readXElement.getSingleTextNotNull("Currency"));//币种
                            retAts7507Detail.noteMoney=readXElement.getSingleTextNotNull("Amount").trim();//金额
                            retAts7507Detail.rate=readXElement.getSingleTextNotNull("Rate").trim();//利率(%)
                            retAts7507Detail.interestMoney="";//利息
                            String status=readXElement.getSingleTextNotNull("Status").trim();//状态
                            retAts7507Detail.depositStates=getBankConfig().getMapValueByKey("statuMapping",status);//存单状态 (正常`1,冻结`2,未启用`3,已退票`4,销户`5,挂失`7,损益`6,作废`9)
                            retAts7507Detail.dumpFlag="";//到期处理方式 (不转存`1,本息转存`2,本金转存，利息转活期`3)
                            retAts7507Detail.drawDate=retAts7507Detail.endDate;//上划日期
                            retAtsTrans.writeRetDetail(retAts7507Detail);
                        }
                    }else{
                        tmpRespCode = "-1";
                        tmpRespInfo = RespInfo.toString();
                    }
                }else{
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            }else{
                tmpRespCode = "-1";
                tmpRespInfo = bankResult[1];
            }
        }catch (Exception e){
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode)) {
            retAtsTrans = new RetAts7507DepQry(p_req7507.reqHeader);
            retAtsTrans.writeStart();//写返回ATS的统一头部信息
            retAtsTrans.writeRespCode(tmpRespCode);
            retAtsTrans.writeRespInfo(tmpRespInfo);
        }else if(retAtsTrans.getWrittenRdCount() == 0) {
            retAtsTrans.writeRespCode("0");
            retAtsTrans.writeRespInfo("交易为空");
        }
        retAtsTrans.writeEnd();
        return retAtsTrans;
    }
}
